//
//  MCNewMarketingEditSKUViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/27.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCNewMarketingEditSKUViewControllerClosure = (MCNewMarketingAddGoodsModel) -> Void

class MCNewMarketingEditSKUViewController: MCViewController {
    
    public var model = MCNewMarketingAddGoodsModel()
    
    public var closure : MCNewMarketingEditSKUViewControllerClosure?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        managerClosure()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Closure
    func managerClosure() {

    }
    // MARK: - Action
    @objc func rightItemClicked() {
        
        self.view.endEditing(true)
        

        var price = Int.max
        var count = 0
        
        
        for productModel in dataArray {
            let productModelTemp = productModel as! MCNewMarketingEditSKUModel
            
            if productModelTemp.salePrice.intValue < price {
                price = productModelTemp.salePrice.intValue
            }
            
            count += productModelTemp.inventoryNum.intValue
        }
        model.productList = dataArray

        
        closure?(model)
        self.navigationController?.popViewController(animated: true)
    }
    // MARK: - Network
    func sendNetworking() {
        
        dataArray = model.productList
        self.tableView.reloadData()
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "编辑商品"
        self.view.backgroundColor = UIColor.white
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("保存", target: self, selector: #selector(rightItemClicked))        
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
        
    }
    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.onDrag
        return tableView
    }()
    
    lazy var dataArray: NSArray = {
        let arrayM = NSArray()
        return arrayM
    }()
}

extension MCNewMarketingEditSKUViewController : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 160
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                
        
        let cell = MCNewMarketingEditSKUCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.model = self.dataArray[indexPath.row] as! MCNewMarketingEditSKUModel
        
        
        return cell
    }
}

